<!--
 * @Descripttion: GGB
 * @version: 1.0
 * @Author: GGB
 * @Date: 2021-12-02 09:21:48
 * @LastEditors: GGB
 * @LastEditTime: 2021-12-08 17:28:46
-->
<template lang="html">
  <div class="search-main">
    <!-- 内容部分 -->
    <div class="search-content" ref="content"  v-infinite-scroll="loadMore"  v-infinite-scroll-disabled="scroll"  v-infinite-scroll-distance="10" v-loading="loading">
      <div class="search-product-item" @click="handleCoupon(product)" v-for="(product,index) in products" :key="index">
        <el-image style="width:263px;height:263px;flex-shrink:1;" fit="cover" :src="product.pict_url+'_270x270xzq90.jpg_.webp'" />
        <div class="price">
            <span class="origin">￥{{product.zk_final_price}}</span>
            <span class="coupon">券后价: {{product.commission_rate}}</span>
        </div>
      <div class="amount">省{{product.coupon_amount}}元</div>
        <span>{{product.volume}} ·人已购买</span>
        <div class="title">{{product.title}}</div>
        <div class="content">
            <el-button type="warning" v-if="product.coupon_info"   class="discovery-buy-btn">领券购买</el-button>
            <span class="origin" >{{product.coupon_info?'原价:59':'晚了，无优惠券'}}} </span>
        </div>
      </div>
    </div>
    <!-- <el-backtop target=".sell-main" :bottom="120">
    <div
      style="{
        height: 100%;
        width: 100%;
        background-color: #f2f5f6;
        box-shadow: 0 0 6px rgba(0,0,0, .12);
        text-align: center;
        line-height: 40px;
        color: #1989fa;
      }"
    >
      UP
    </div>
  </el-backtop> -->
  </div>
</template>

<script>
import { getSearch } from "@/api/search";
import {
  infiniteScroll,
  infiniteScrollDisabled,
  infiniteScrollDistance,
} from "@/directive/scroll";
export default {
  name: "Search", //使用自定义指令
  data() {
    return {
      products: [],
      loading: false,
      scroll: false,
      page: 1,
      keyword: "",
    };
  },
  //使用自定义指令
  directives: {
    infiniteScroll,
    infiniteScrollDisabled,
    infiniteScrollDistance,
  },
  mounted() {
    this.keyword = $nuxt.$route.query.keyword || "";
  },
  methods: {
    up() {
      window.scrollTo({
        top: 0,
        behavior: "smooth",
      });
    },
    loadMore() {
      if (this.loading) return;
      this.page++;
      this.loading = true;
      getSearch({ page: this.page, keyword: this.keyword })
        .then((res) => {
          this.products = [
            ...this.products,
            ...res.data.tbk_dg_material_optional_response.result_list.map_data,
          ];
        })
        .catch((err) => {
          this.page--;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleProduct(item) {
      window.open(item.click_url, "_blank");
    },
    handleCoupon(item) {
      window.open(item.coupon_click_url, "_blank");
    },
  },
  //   加载数据
  async asyncData({ params, query }) {
    let { keyword } = query;
    let { data: products } = await getSearch({ page: 1, keyword });
    return {
      products: products.tbk_dg_material_optional_response.result_list.map_data,
    };
  },
};
</script>

<style lang="scss" scope>
.search-main {
  width: 1140px;
  min-height: 83vh;
  // position: relative;
  // overflow-y: auto;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  .search-content::-webkit-scrollbar {
    display: none; /*隐藏滚动条*/
  }
  .search-content {
    width: 1140px;
    box-sizing: border-box;
    display: flex;
    flex: 1;
    height: 100%;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    // overflow-y: auto;
    padding: 10px;
    .search-product-item {
      width: 275px;
      position: relative;
      box-sizing: border-box;
      display: flex;
      flex-direction: column;
      align-items: flex-start;
      justify-content: flex-start;
      padding: 5px;
      background: #fff;
      box-shadow: 0 5px 10px #d4d4d4;
      margin-bottom: 15px;
      border-radius: 5px;
      cursor: pointer;
      &:hover {
        box-shadow: 0 2px 5px #f40;
      }
      .amount {
        position: absolute;
        background: #c9302c;
        color: #fff;
        padding: 5px 18px;
        top: 5px;
        right: 5px;
      }
      .title {
        font-weight: 600;
        min-height: 43px;
        font-size: 16px;
        color: #000;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
        margin-bottom: 5px;
      }
      .price {
        display: flex;
        flex-direction: row;
        align-items: center;
        color: #f40;
        margin: 10px 0;
        font-size: 18px;
        .origin {
          text-decoration: line-through;
        }
        .coupon {
          margin-left: 10px;
        }
      }

      .content {
        display: flex;
        flex-direction: row;
        align-items: center;
        justify-content: flex-start;
        font-weight: 600;
        margin-top: 10px;
        margin-bottom: 10px;
        font-size: 16px;
        width: 100%;
        .discovery-buy-btn {
          text-decoration: none;
          border-radius: 5px;
          align-self: flex-end;
          background-color: #f56c6c;
          border-color: #f56c6c;
          color: #fff;
          padding: 8px 20px;
          margin-right: 10px;
        }
        .origin {
          font-weight: 600;
          color: #ebb563;
        }
      }
    }
  }
}
</style>
